package com.api.gbhg.liuwenbo.version3.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.version2.api.AdministrationApi;
import com.api.gbhg.liuwenbo.version2.api.BudgetSectionIncomeApi2;
import com.api.gbhg.liuwenbo.version2.api.CauseApi;
import com.api.gbhg.liuwenbo.version2.api.NonFinanceApi;
import com.api.gbhg.liuwenbo.version2.pojo.BudgetSectionIncome2;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.time.LocalDate;
import java.util.*;

/**
 * @Description 非财政拨款查询
 * @Verseion 3.0
 * @Author lwb
 * @Date
 */
@Path("v3/NonFinance")
public class NonFinanceApi3 {

    private NonFinanceApi nonFinanceApi = new NonFinanceApi();

    @POST
    @Path("/getNonFinance")
    @Produces(MediaType.APPLICATION_JSON)
    public String getNonFinance(Map param){
        Map msgMap = new HashMap<>();

        if (param == null ||!param.containsKey("nf") || !param.containsKey("type") || !param.containsKey("dw")){
            msgMap.put("code","500");
            msgMap.put("msg","缺少参数 请检查参数是否完整！");
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }

        param.put("jz","yqndyjjzje");
        Integer nf = param.get("nf").equals("")? LocalDate.now().getYear():Integer.parseInt(param.get("nf").toString());
        param.put("nf",nf);

        //单位条件
        String dwid = "";
        boolean dwCondition = false;
        if ("xzdw".equals(param.get("type"))){
            //行政单位
            dwCondition = true;
            Object dw = param.get("dw");
            if ("".equals(dw)){
                dwid = AdministrationApi.getdw();
            }else{
                dwid = AdministrationApi.getdw(dw.toString());
            }
        }else if("sydw".equals(param.get("type"))){
            //事业单位
            dwCondition = true;
            Object dw = param.get("dw");
            if ("".equals(dw)){
                dwid = CauseApi.getdw();
            }else{
                dwid = CauseApi.getdw(dw.toString());
            }
        }

        //返回结果集
        Map<String, List<BudgetSectionIncome2>> resultMap = new HashMap<>();
        try {
            Map<Integer, String> fzyDwIdName = nonFinanceApi.getFzyDwIdName(dwCondition, dwid);
            Set<Map.Entry<Integer, String>> entries = fzyDwIdName.entrySet();
            Iterator<Map.Entry<Integer, String>> iterator = entries.iterator();
            while (iterator.hasNext()) {
                Map.Entry<Integer, String> entry = iterator.next();
                param.put("dw", entry.getKey());
                param.put("dwName", entry.getValue());
                List<BudgetSectionIncome2> fczbksrData = new BudgetSectionIncomeApi2().getFczbksrData(param);
                resultMap.put(entry.getValue() + "_" + entry.getKey(), structureConversion(fczbksrData,String.valueOf(nf)));
            }

            msgMap.put("code","200");
            msgMap.put("msg","");
            msgMap.put("data",resultMap);
            return JSON.toJSONString(msgMap);
        }catch (Exception e){
            e.printStackTrace();
            msgMap.put("code","500");
            msgMap.put("msg","500 程序异常,请联系管理员！"+e.getMessage());
            return JSON.toJSONString(msgMap);
        }
    }

    /**
     * 结构变更
     * @param paramList
     *          数据集合
     * @param nf
     *          年份
     * @return
     */
    public static List<BudgetSectionIncome2> structureConversion(List<BudgetSectionIncome2> paramList, String nf){
        List<BudgetSectionIncome2> list = new LinkedList<>();

        for (int i=0;i<paramList.size();i++){
            BudgetSectionIncome2 bean = paramList.get(i);
            bean.setNf(nf);
            List<BudgetSectionIncome2> xjBean = bean.getXj();
            if (xjBean != null && xjBean.size()>0){
                for (int j = 0; j < xjBean.size(); j++) {
                    BudgetSectionIncome2 tempBean = xjBean.get(j);
                    xjChange(tempBean,bean,nf,list);
                }
            }
        }
        return list;
    }

    /**
     * 下级结构转换
     * @param xjBean
     *          下级bean
     * @param bean
     *          收入分类bean
     * @param nf
     *          年份
     * @param list
     *          目标集合
     */
    public static void xjChange(BudgetSectionIncome2 xjBean,BudgetSectionIncome2 bean,String nf,List<BudgetSectionIncome2> list){
        xjBean.setDwId(bean.getDwId());
        xjBean.setDwName(bean.getDwName());
        xjBean.setNf(nf);

        String dataid = xjBean.getId();
        xjBean.setId(bean.getId());
        xjBean.setName(bean.getName());
        xjBean.setDataid(dataid);

        list.add(xjBean);
    }
}
